<template>
  <a-modal
    :title="title"
    :width="460"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="取消"
    okText="保存">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="纽扣选择"
          hasFeedback >
          <a-select v-model="select_material" style="color: #827d7d" placeholder="请选择纽扣">
            <a-select-option :value="item.id" v-for="item in categoryList" >{{ item.materialCode }}</a-select-option>
          </a-select>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="备注"
          hasFeedback >
          <a-textarea placeholder="请输入备注信息" v-model="remark" rows="3"/>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { httpAction, postAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"
  import JImageUpload from '../../../components/jeecg/JImageUpload'
  import Qs from "qs";

  export default {
    name: "JeecgDemoModal",
    components: {
      JImageUpload
    },
    data () {
      return {
        select_material:'请选择纽扣',
        remark:'',
        categorydetail:[],
        categoryList:[],
        title:"操作",
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
        },
        url: {
          fileUpload: window._CONFIG['domianURL'] + "/sys/common/upload",
          query_detail:"/admin/formalplate/query_all_materialInfo",
          add: "/admin/testplate/set_plate_material"
        },
      }
    },
    computed: {
      uploadAction: function() {
        return this.url.fileUpload;
      }
    },
    created () {
    },
    methods: {
      edit (record) {
        this.select_material='请选择纽扣';
        this.remark='';
        this.model = Object.assign({}, record);
        this.visible = true;//开启弹框
        if(record=="0"){
          this.title="配置纽扣"
          this.show_categorydetail(2);
        }else {
          this.title="配置纽扣"
        }
      },
      show_categorydetail(id) {
        let params = {type:id};
        getAction(this.url.query_detail,params).then((res)=>{
          if(res.success){
            this.categoryList = res.result;
          }else {
            this.$message.error("查询失败");
          }
        });
      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if(this.select_material=='请选择纽扣'){
            this.$message.warning("请选择纽扣");
            return;
          }
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            let params = '';
            if(this.model.id!=null && this.model.id!=''){
              httpurl+=this.url.edit;
              method = 'put';
              params = {xiuziLocaltion:this.category_name,code:this.category_code,
                singlePrice2:this.single_price,teamPrice2:this.team_price,id:this.model.id}
            }else{
              httpurl+=this.url.add;
              method = 'post';
              params = {otherId:this.select_material,isLibu:"2",categoryId:this.$route.query.category_id,
                remark:this.remark}
            }
            httpAction(httpurl,params,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
        })
      },
      handleCancel () {
        this.close()
      },
    }
  }
</script>

<style scoped>

</style>